﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SanitationTcpGw
{
    /// <summary>
    /// 808流水号管理
    /// </summary>
    public class P808SerialMgr
    {
        static UInt16 _serial = 0;

        static object _sync = new object();

        private P808SerialMgr() { }

        /// <summary>
        /// 产生一个id
        /// </summary>
        public static UInt16 IdGen
        {
            get
            {
                lock (_sync)
                {
                    _serial++;
                    if (_serial != 0)
                    {
                        return _serial;
                    }

                    _serial = 1;
                    return _serial;
                }
            }
        }
    }

    /// <summary>
    /// 809流水号管理
    /// </summary>
    public class P809SerialMgr
    {
        static UInt32 _serial = 0;

        static object _sync = new object();

        private P809SerialMgr() { }

        /// <summary>
        /// 产生一个id
        /// </summary>
        public static UInt32 IdGen
        {
            get
            {
                lock (_sync)
                {
                    _serial += 2;

                    return _serial;
                }
            }
        }
    }

    /// <summary>
    /// 参数缓存
    /// </summary>
    public class ParamCache
    {
        static ParamCache _inst = null;

        Dictionary<int, string> _cache = null;

        private ParamCache()
        {
            _cache = new Dictionary<int, string>();
        }

        public static ParamCache Instance
        {
            get
            {
                if (_inst == null)
                {
                    _inst = new ParamCache();
                }
                return _inst;
            }
        }

        public void Set(int key, string val)
        {
            lock (_cache)
            {
                _cache[key] = val;
            }
        }

        public string Get(int key)
        {
            string ret = string.Empty;
            lock (_cache)
            {
                try
                {
                    ret = _cache[key];
                }
                catch
                {
                }
                return ret;
            }
        }

        public void Remove(int key)
        {
            lock (_cache)
            {
                try
                {
                    _cache.Remove(key);
                }
                catch
                {

                }
            }
        }
    }

    /// <summary>
    /// 请求类型
    /// </summary>
    public enum REQ_TYPE
    {
        PHOTO = 0, //拍照
        TRAVEL, //行驶记录
        EWAYBILL, //电子运单

        REQ_END,
    }

    /// <summary>
    /// 请求队列
    /// </summary>
    public class ReqQue
    {
        static ReqQue _inst = null;

        Dictionary<string, Stack<string>[]> _reqQue = null;

        public static ReqQue Instance
        {
            get
            {
                if (_inst == null)
                {
                    _inst = new ReqQue();
                }
                return _inst;
            }
        }

        private ReqQue()
        {
            _reqQue = new Dictionary<string, Stack<string>[]>();
        }

        public void Enque(string devid, REQ_TYPE type, string item)
        {
            int total = (int)REQ_TYPE.REQ_END;
            int tp = (int)type;

            lock (_reqQue)
            {
                if (!_reqQue.ContainsKey(devid))
                {
                    Stack<string>[] ques = new Stack<string>[total];

                    for (int i = 0; i < total; i++)
                    {
                        Stack<string> que = new Stack<string>();
                        ques[i] = que;
                    }
                    _reqQue[devid] = ques;
                }

                _reqQue[devid][tp].Push(item);
            }
        }

        public string Deque(string devid, REQ_TYPE type)
        {
            string ret = "";
            int tp = (int)type;

            lock (_reqQue)
            {
                try
                {
                    if (!_reqQue.ContainsKey(devid))
                    {
                        return "";
                    }

                    //has the key
                    ret = _reqQue[devid][tp].Pop();
                }
                catch
                {
                }
            }

            return ret;
        }
    }
}
